package com.wanjia.bpsapi.controller;


import com.wanjia.bpsapi.exception.*;
import com.wanjia.bpsapi.pojo.model.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;


public class BaseController {
    /*操作成功的状态码*/
    public static final int OK = 200;

    @ExceptionHandler(value = ControllerException.class)//用于统一处理抛出的异常
    public JsonResult<Object> handleException(Throwable e) {
        JsonResult<Object> result = new JsonResult<>(e);

        if (e instanceof UserNotFoundException) {
            result.setState(4000);
            result.setMessage("用户数据不存在");
        }else if (e instanceof PasswordNotMatchException) {
            result.setState(4001);
            result.setMessage("密码错误");
        }else if (e instanceof DataExistException){
            result.setState(4002);
            result.setMessage("此ID数据已存在，可修改数据");
        }else if (e instanceof UpdateException){
            result.setState(4003);
            result.setMessage("更新数据发生错误");
        }else if (e instanceof DeleteException){
            result.setState(4004);
            result.setMessage("删除数据发生错误");
        }else if (e instanceof TokenstateException){
            result.setState(5000);
        }else if (e instanceof AddDataException){
            result.setState(4005);
            result.setMessage("新增数据发生错误");
        }
        return result;
    }
}
